<template>
	<view>
		<view class="paying">
			支付中...
		</view>

		<view @click="toaddress" class="item">
			<view class="left">
				<view class="name">
					<text>{{addresslist.name}}</text><text class="default" v-if="addresslist.defaultStatus">默认</text>
				</view>
				<view class="tel">
					{{addresslist.phoneNumber}} <text style="margin-left: 40rpx;">{{addresslist.city}} {{addresslist.detailAddress}}</text>
				</view>
			</view>
		</view>


		<view class="order-list">
			<view class="orderitem">
				<view class="top">
					<text>订单编号:{{id}}</text>
					<text class="status">未付款</text>
				</view>

				<view class="con" v-for="con in datalist.orderItems" :key='con.id'>
					<image :src="con.productPic" mode=""></image>
					<view class="info">
						<view class="left">
							<view class="title">{{con.productName}}</view>
							<view class="">编号: 123456789</view>
							<view class="">规格: 暂无</view>
							<view class="color">颜色: {{JSON.parse(con.productAttr)[0].value ? JSON.parse(con.productAttr)[0].value : '暂无'}}</view>
						</view>
						<view class="left right">
							<view class="title">￥{{con.productPrice}}</view>
							<view class="">X{{con.productQuantity}}</view>
						</view>
					</view>
				</view>
				<view class="bottom">
					<text>时间:{{createTime}}</text>
					<text>合计: ￥{{totalAmount}}</text>
				</view>
			</view>
		</view>
		<button type="default" @click="payfor">确认付款</button>
	</view>
</template>

<script>
	import {
		findAllAddress
	} from "../../../api/my/address/address.js"
	import {
		addPreOrder,
		getPreOrderById,
		addConfirmOrder,
		payConfirm,
		receiveDone,
		deleteOrder
	} from "../../../api/my/order/order.js"
	export default {
		data() {
			return {
				datalist: '',
				addresslist: '',
				addressid: '',
				id: '',
				createTime: '',
				totalAmount: '',
			};
		},
		methods: {
			// 到收货地址页面
			toaddress() {
				uni.navigateTo({
					url: '../address/address?change=1',
				});
			},
			// 确认付款
			payfor() {
				var orderinfo = {
					"addressId": this.addressid,
					"orderId": this.datalist.orderBase.id,
					"orderItemList": this.datalist.orderItems,
					"payType": 0,
					"sourceType": 1
				}
				addConfirmOrder(orderinfo).then(res => {
					console.log(res);
					if (res.success) {
						
						uni.setStorageSync('orderinfo', JSON.stringify(orderinfo))
						uni.removeStorageSync('address')
						// 到订单支付页面
						uni.navigateTo({
							url: '../payfor/payfor'
						})
					}
				})
			}
		},
		onLoad(e) {
			// 获取订单信息
			console.log(e);
			getPreOrderById(e.id).then(res => {
				console.log(res);
				this.datalist = res.data
				this.id = res.data.orderBase.id
				this.createTime = res.data.orderBase.createTime
				this.totalAmount = res.data.orderBase.totalAmount
			})

		},
		onShow() {
			// 获取收货地址
			findAllAddress({}).then(res => {
				console.log(res);
				var add = uni.getStorageSync('address')
				console.log(add);
				if (add) {
					add = JSON.parse(uni.getStorageSync('address'))
					this.addresslist = add
					this.addressid = add.id
					console.log(addresslist);
				} else {
					this.addresslist = res.data.items.filter(el => {
						return el.defaultStatus == 1
					})
					this.addresslist = this.addresslist[0]
					this.addressid = this.addresslist.id
				}
			})
		}
	}
</script>

<style lang="scss" scoped>
	.default {
		background-color: #354e44;
		padding: 6rpx 20rpx;
		border-radius: 6rpx;
		color: white;
		margin-left: 40rpx;
		font-size: 20rpx;
		box-sizing: border-box;

	}

	.item {
		width: 710rpx;
		height: 160rpx;
		background-color: #FFFFFF;
		border-radius: 20rpx;
		display: flex;
		justify-content: space-between;
		box-sizing: border-box;
		padding: 0 20rpx;
		margin: 20rpx auto;

		.switch {
			padding: 20rpx 0;
			margin-top: 10rpx;
			box-sizing: border-box;
		}

		.left {

			.name {
				margin-top: 34rpx;
				font-size: 28rpx;
				box-sizing: border-box;
			}

			.tel {
				margin-top: 10rpx;
				box-sizing: border-box;
				font-size: 24rpx;
				color: #999;
			}
		}

		image {
			width: 40rpx;
			height: 40rpx;
			box-sizing: border-box;
			margin: 60rpx;
		}
	}

	button {
		width: 710rpx;
		margin: 0 auto;
		background-color: #fff;
		box-sizing: border-box;
	}

	.paying {
		width: 710rpx;
		margin: 0rpx auto;
		margin-bottom: 20rpx;
		margin: 20rpx;
		text-indent: 20rpx;
		box-sizing: border-box;
		border-bottom: 2rpx solid #e5e5e5;
		background-color: #fff;
		border-radius: 12rpx;
		line-height: 106rpx;
	}

	.orderitem {
		width: 710rpx;
		background-color: #FFFFFF;
		border-radius: 20rpx;
		box-sizing: border-box;
		padding: 20rpx 20rpx;
		margin: 40rpx auto;

		.top {
			display: flex;
			justify-content: space-between;
			padding: 18rpx 0;
			border-bottom: 2rpx solid #eee;
			box-sizing: border-box;
			font-size: 24rpx;
			color: #3e3e3e;

			.status {
				color: #034c46;
				box-sizing: border-box;
			}
		}

		.con {
			padding: 30rpx 0;
			display: flex;
			justify-content: space-between;
			box-sizing: border-box;
			border-bottom: 2rpx solid #eee;

			image {
				width: 160rpx;
				height: 160rpx;
				border-radius: 20rpx;
				box-sizing: border-box;
			}

			.info {
				width: 460rpx;
				display: flex;
				justify-content: space-between;
				box-sizing: border-box;
				margin-bottom: 20rpx;

				.left {
					font-size: 22rpx;
					line-height: 30rpx;
					box-sizing: border-box;
					color: #8d8d8d;

					.title {
						width: 400rpx;
						font-size: 26rpx;
						color: #3e3e3e;
						line-height: 58rpx;
						box-sizing: border-box;
						overflow: hidden;
						text-overflow: ellipsis;
						white-space: nowrap;
					}
				}

				.right {
					text-align: right;
					box-sizing: border-box;
				}
			}
		}

		.bottom {
			padding: 20rpx 0;
			display: flex;
			box-sizing: border-box;
			justify-content: space-between;
			font-size: 26rpx;
			line-height: 36rpx;
		}

		.btns {
			padding: 12rpx 0;
			height: 80rpx;
			box-sizing: border-box;

			.btn {
				height: 54rpx;
				line-height: 50rpx;
				float: right;
				box-sizing: border-box;
				margin-left: 20rpx;
				font-size: 24rpx;
				border: 2rpx solid #2d4f43;
				color: #2d4f43;
				background-color: rgba(0, 0, 0, 0);
			}
		}

	}

	.orderitem {
		margin-top: 20rpx;
		box-sizing: border-box;
	}
</style>
